<?php
	require MODULE_ROOT . '/helpers/functions.php';
	$config = config();
	$rates = explode('|', $config['rate']);
 	global $_GPC, $_W;
	$ops = array('index', 'delete','confirm','cancel','remote','reject','fencheng_confirm'); // 只支持此 3 种操作.
	$op = in_array($_GPC['op'], $ops) ? $_GPC['op'] : 'index';

	if($op=='index'){
		#查询办卡信息
		$condition='res.uniacid ='.$_W['uniacid'];
		if($_GPC['lend']){
	    	$bank = $_GPC['lend'];
	    	$condition .=" and res.bank like '%{$_GPC['lend']}%' "; 
	    }
		if($_GPC['username']){
	    	$username = $_GPC['username'];
	    	$condition .=" and usr.name like '%{$_GPC['username']}%' "; 
	    }
		if($_GPC['mobile']){
	    	$mobile = $_GPC['mobile'];
	    	$condition .=" and usr.mobile like '%{$_GPC['mobile']}%' "; 
	    }
		//显示第几页的数据
		$pindex = max(1, intval($_GPC['page']));
		//每页显示的条数
	    $psize = 10;
		//总条数  

	    $count = pdo_fetchcolumn("select count(res.id) from ".tablename('d1sj_card_record')
		." as res left join ".tablename('d1sj_card_user')
		." as usr on res.uid=usr.uid "
		." where ".$condition." and res.type =2");
		$list = pdo_fetchall("select res.*,usr.name,usr.mobile from ".tablename('d1sj_card_record')
			." as res left join ".tablename('d1sj_card_user')
			." as usr on res.uid=usr.uid "
			." where ".$condition." and res.type =2 order by res.id desc  limit ". ($pindex - 1) * $psize . ',' . $psize);

		$pager = pagination($count, $pindex, $psize);
		$lend=pdo_fetchall('select title,id from  '.tablename('d1sj_card_lend')." where uniacid=:uniacid",array(':uniacid'=>$_W['uniacid']));
		
	}


	if($_GPC['id']){
		$id=$_GPC['id'];
		$record_res=pdo_fetch('select * from'.tablename('d1sj_card_record')." where id=:id",array(':id'=>$id));
		if(!$record_res){
			message('没有对应的数据', $this->createWebUrl('netloan', array('op' => 'index')), 'error');
		}
		if($op=='confirm'){
			#确认返佣给用户对应的上级返佣
			$usre_uid1=pdo_fetch('select * from '.tablename('d1sj_card_user')." where uid=:uid and uniacid=:uniacid",array(':uid'=>$record_res['fan_uid1'],':uniacid'=>$_W['uniacid']));
			$usre_uid2=pdo_fetch('select * from '.tablename('d1sj_card_user')." where uid=:uid and uniacid=:uniacid",array(':uid'=>$record_res['fan_uid2'],':uniacid'=>$_W['uniacid']));
			$usre_uid3=pdo_fetch('select * from '.tablename('d1sj_card_user')." where uid=:uid and uniacid=:uniacid",array(':uid'=>$record_res['fan_uid3'],':uniacid'=>$_W['uniacid']));
			if($usre_uid1){
				$usre_data1=array(
					'money'=>$usre_uid1['money']+$record_res['fan_level1'],
				);
				pdo_update('d1sj_card_user',$usre_data1,array('id'=>$usre_uid1['id']));
			}
			if($usre_uid2){
				$usre_data2=array(
					'money'=>$usre_uid2['money']+$record_res['fan_level2'],
				);
				pdo_update('d1sj_card_user',$usre_data2,array('id'=>$usre_uid2['id']));
			}
			if($usre_uid3){
				$usre_data3=array(
					'money'=>$usre_uid3['money']+$record_res['fan_level3'],
				);
				pdo_update('d1sj_card_user',$usre_data3,array('id'=>$usre_uid3['id']));
			}
			$record_data=array(
				'status'=>1,
			);
			$record_result=pdo_update('d1sj_card_record',$record_data,array('id'=>$id));
			#发送模板消息
			$config=$this->config();
			$record_res_list=pdo_fetch('select * from '.tablename('d1sj_card_record')."where id=:id",array(':id'=>$id));
			$user_list_openid=pdo_fetch('select openid from '.tablename('d1sj_card_user')." where uid=:uid and uniacid=:uniacid",array(':uid'=>$record_res_list['uid'],':uniacid'=>$_W['uniacid']));

			$lend_resd=pdo_fetch('select title from '.tablename('d1sj_card_lend')."where uniacid=:uniacid and id =:id ",array(':uniacid'=>$_W['uniacid'],':id'=>$record_res_list['bank']));

			$res=$this->to_examine_muban($user_list_openid['openid'],$config['sh_template_id'],$lend_resd['title'],$record_res_list['id']);

			if($record_result){
				message('修改成功', $this->createWebUrl('netloan', array('op' => 'index')), 'success');
			}
		}

		if($op=='reject'){
			$record_data=array(
				'status'=>2,
			);
			$record_result=pdo_update('d1sj_card_record',$record_data,array('id'=>$id));
			if($record_result){
				message('修改成功', $this->createWebUrl('netloan', array('op' => 'index')), 'success');
			}
		}
		#删除
		if($op=='delete'){
			$id=$_GPC['id'];
			$record_delete=pdo_delete('d1sj_card_record',array('id'=>$id));
			if($record_delete){
				message('删除成功', $this->createWebUrl('netloan', array('op' => 'index')), 'success');
			}
		}
					#如果分成金额是百分百添加下款金额按比例分成
	if($op=='fencheng_confirm'){
			$id=$_GPC['id'];
			$card_record=pdo_fetch('select * from '.tablename('d1sj_card_record')." where id=:id and uniacid=:uniacid",array(':id'=>$id,':uniacid'=>$_W['uniacid']));
			if($card_record){
				$card_lend=pdo_fetch('select price,percent_price from '.tablename('d1sj_card_lend')." where id=:id and uniacid=:uniacid",array(':id'=>$card_record['bank'],':uniacid'=>$_W['uniacid']));
			}
	}
	#提交修改数据
	if(checksubmit()){
		$id=$_GPC['id'];
		$record_res=pdo_fetch('select * from'.tablename('d1sj_card_record')." where id=:id",array(':id'=>$id));


		#接受一个分成金额
		$application_amount=$_GPC['application_amount'];
		 #接收分成比例
		$percent_price=$_GPC['percent_price'];
		$bankMoney=$application_amount*($percent_price/100);
		$data['fan_total'] = $bankMoney;
		$uid=$_GPC['uid'];
		$user_list=pdo_fetch('select * from '.tablename('d1sj_card_user')." where uid=:uid and uniacid=:uniacid",array(':uid'=>$uid,':uniacid'=>$_W['uniacid']));

		if($user_list['leader1']) {
			$user_list1=pdo_fetch('select * from '.tablename('d1sj_card_user')." where uid=:uid and uniacid=:uniacid",array(':uid'=>$user_list['leader1'],':uniacid'=>$_W['uniacid']));
	 	    $sql = 'SELECT * FROM ' . tablename('d1sj_card_group') . ' WHERE id = :id';
			$group_info1 = pdo_fetch($sql, [':id' => $user_list1['group']]);
            $groupLevel1 =$group_info1 ?$group_info1['mlevel'] : 0;
        }
        if($user_list['leader2']) {
        	$user_list2=pdo_fetch('select * from '.tablename('d1sj_card_user')." where uid=:uid and uniacid=:uniacid",array(':uid'=>$user_list['leader2'],':uniacid'=>$_W['uniacid']));
	 	    $sql = 'SELECT * FROM ' . tablename('d1sj_card_group') . ' WHERE id = :id';
			$group_info2 = pdo_fetch($sql, [':id' => $user_list2['group']]);
			$groupLevel2 =$group_info2 ?$group_info2['mlevel'] : 0;
        }
        if($user_list['leader3']) {
            $user_list3=pdo_fetch('select * from '.tablename('d1sj_card_user')." where uid=:uid and uniacid=:uniacid",array(':uid'=>$user_list['leader3'],':uniacid'=>$_W['uniacid']));
	 	    $sql = 'SELECT * FROM ' . tablename('d1sj_card_group') . ' WHERE id = :id';
			$group_info3 = pdo_fetch($sql, [':id' => $user_list3['group']]);
			$groupLevel3 =$group_info3 ?$group_info3['mlevel'] : 0;

        }
        if($user_list['leader1'] && $rates[0] && $groupLevel1 >= 1) {
            $data['fan_level1'] = $bankMoney * ($rates[0] / 100);
        }
        if($user_list['leader2'] && $rates[1] && $groupLevel2 >= 2) {
            $data['fan_level2'] = $bankMoney * ($rates[1] / 100);
        }
   

        if($user_list['leader3'] && $rates[2] && $groupLevel3 >= 3) {
            $data['fan_level3'] = $bankMoney * ($rates[2] / 100);
        }

    	#确认返佣给用户对应的上级返佣
		$usre_uid1=pdo_fetch('select * from '.tablename('d1sj_card_user')." where uid=:uid and uniacid=:uniacid",array(':uid'=>$record_res['fan_uid1'],':uniacid'=>$_W['uniacid']));
		$usre_uid2=pdo_fetch('select * from '.tablename('d1sj_card_user')." where uid=:uid and uniacid=:uniacid",array(':uid'=>$record_res['fan_uid2'],':uniacid'=>$_W['uniacid']));
		$usre_uid3=pdo_fetch('select * from '.tablename('d1sj_card_user')." where uid=:uid and uniacid=:uniacid",array(':uid'=>$record_res['fan_uid3'],':uniacid'=>$_W['uniacid']));

		if($usre_uid1){
			$usre_data1=array(
				'money'=>$usre_uid1['money']+$record_res['fan_level1'],
			);
			pdo_update('d1sj_card_user',$usre_data1,array('id'=>$usre_uid1['id']));
		}
		if($usre_uid2){
			$usre_data2=array(
				'money'=>$usre_uid2['money']+$record_res['fan_level2'],
			);
			pdo_update('d1sj_card_user',$usre_data2,array('id'=>$usre_uid2['id']));
		}
		if($usre_uid3){
			$usre_data3=array(
				'money'=>$usre_uid3['money']+$record_res['fan_level3'],
			);
			pdo_update('d1sj_card_user',$usre_data3,array('id'=>$usre_uid3['id']));
		}
	
        #审核成功
        $data['status']=1;
        $record_res = pdo_update('d1sj_card_record', $data,['id'=>$id]);
    	if($record_res){
    		#发送模板消息
			$record_res_list=pdo_fetch('select * from '.tablename('d1sj_card_record')."where id=:id",array(':id'=>$id));
			$user_list_openid=pdo_fetch('select openid from '.tablename('d1sj_card_user')." where uid=:uid and uniacid=:uniacid",array(':uid'=>$record_res_list['uid'],':uniacid'=>$_W['uniacid']));
			$lend_resd=pdo_fetch('select title from '.tablename('d1sj_card_lend')."where uniacid=:uniacid and id =:id ",array(':uniacid'=>$_W['uniacid'],':id'=>$record_res_list['bank']));
			$res=$this->to_examine_muban($user_list_openid['openid'],$config['sh_template_id'],$lend_resd['title'],$record_res_list['id']);
			message('审核成功', $this->createWebUrl('netloan', array('op' => 'index')), 'success');
		}

	}


	}


	include $this->template('web/netloan');


?>